package common

import (
	"organoid_public/domain/common"
	commonService "organoid_public/domain/common/service"
	"organoid_public/pkg/logger"
)

type service struct {
	log           logger.Logger
	commonService commonService.Service
}

func newService(log logger.Logger) (*service, error) {
	commonSvc, err := common.Service()
	if err != nil {
		return nil, err
	}
	return &service{
		log:           log,
		commonService: commonSvc,
	}, nil
}

func (s *service) GetEnum(request *EnumReq) (map[string][]EnumRes, error) {
	data, err := s.commonService.GetEnum(request.Enum)
	if err != nil {
		return nil, err
	}
	res := make(map[string][]EnumRes)
	for _, datum := range data {
		res[datum.DictionaryCode] = append(res[datum.DictionaryCode], EnumRes{
			Name:  datum.Name,
			Value: datum.Value,
		})
	}

	return res, nil
}

func (s *service) GetEnumWithGroup(request *EnumReq) (map[string]map[string][]EnumRes, error) {
	data, err := s.commonService.GetEnum(request.Enum)
	if err != nil {
		return nil, err
	}
	res := make(map[string]map[string][]EnumRes)
	for _, datum := range data {
		if _, ok := res[datum.DictionaryCode]; !ok {
			res[datum.DictionaryCode] = make(map[string][]EnumRes)
		}
		res[datum.DictionaryCode][datum.Group] = append(res[datum.DictionaryCode][datum.Group], EnumRes{
			Name:  datum.Name,
			Value: datum.Value,
		})
	}
	return res, nil
}
